<%--
  Created by IntelliJ IDEA.
  User: 李肖俊
  Date: 2021/6/17
  Time: 15:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/component/pear/css/pear.css" />
</head>
<%--添加新增的模板--%>
<script type="text/html" id="addTpl">
    <form class="layui-form" action="">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">账号</label>
                        <div class="layui-input-block">
                            <input type="text" name="loginName" lay-verify="title" autocomplete="off" placeholder="请输入账号" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-block">
                            <input type="text"  name="realName" lay-verify="title" autocomplete="off" placeholder="请输入姓名" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-block">
                            <input type="password"  name="loginPassword"  lay-verify="title" autocomplete="off" placeholder="请输入密码" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">确认密码</label>
                        <div class="layui-input-block">
                            <input type="password"  name="repeat-password"  lay-verify="title" autocomplete="off" placeholder="请确定密码" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">电话</label>
                        <div class="layui-input-block">
                            <input type="text"  name="phone" lay-verify="title" autocomplete="off" placeholder="请输入电话" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="address" required  lay-verify="required" placeholder="请输入地址" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">证件号码</label>
                        <div class="layui-input-block">
                            <input type="text" name="idCard" required  lay-verify="required" placeholder="请输入证件号码" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-block">
                            <input type="radio" name="sex" value="0" title="男" checked>
                            <input type="radio" name="sex" value="1" title="女">
                        </div>
                    </div>
                    <%--用户头像的上传的实现--%>
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="test1">上传图片</button>
                        <input id="img" name="img" hidden>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1">
                            <p id="demoText"></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</script>
<%--添加的修改的模板--%>
<script type="text/html" id="updateTpl">
    <form class="layui-form" action="" lay-filter="update-user">
        <div class="mainBox">
            <input name="id" type="text" hidden>
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">账号</label>
                        <div class="layui-input-block">
                            <input type="text" name="loginName" readonly lay-verify="title" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-block">
                            <input type="text"  name="realName" lay-verify="title" autocomplete="off" placeholder="请输入姓名" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">电话</label>
                        <div class="layui-input-block">
                            <input type="text"  name="phone"  lay-verify="title" autocomplete="off" placeholder="请输入电话" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">地址</label>
                        <div class="layui-input-block">
                            <input type="text"  name="address"  lay-verify="title" autocomplete="off" placeholder="请输入地址" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">证件号码</label>
                        <div class="layui-input-block">
                            <input type="text"  name="idCard"  lay-verify="title" autocomplete="off" placeholder="请输入证件号码" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-block">
                            <input type="radio" name="sex" value="0" title="男" checked>
                            <input type="radio" name="sex" value="1" title="女">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="deleted"  lay-text="启用|禁用" lay-skin="switch" value="1">
                        </div>
                    </div>
                    <%--用户头像的上传的实现--%>
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="update-upload">上传图片</button>
                        <input name="img" id="update-img-i" hidden>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="update-img">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</script>
<%--添加分配角色的模板--%>
<script type="text/html" id="assignTpl">
    <form class="layui-form" action="" lay-filter="assign-user">
        <div class="mainBox">
            <input name="id" type="text" hidden>
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色</label>
                        <div class="layui-input-block">
                            <ul id="roleTree" class="dtree"></ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="user-assign">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</script>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <form class="layui-form" action="" onsubmit="return false">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="realName" required   placeholder="请输入姓名" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-inline">
                            <select name="sex"  lay-search>
                                <option value=""></option>
                                <option value="0">男</option>
                                <option value="1">女</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline">
                            <select name="deleted" lay-search>
                                <option value=""></option>
                                <option value="0">禁用</option>
                                <option value="1">启用</option>
                            </select>
                        </div>
                    </div>
                </form>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="assign"><i class="layui-icon layui-icon-set"></i>
    </button>
</script>

<script type="text/html" id="user-enable">
    <input type="checkbox" user-id="{{d.id}}" name="enable" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" {{d.deleted == 1 ? 'checked' : ''}}>
</script>

<script type="text/html" id="user-sex">
    {{#if (d.sex == 0) { }}
    <span>男</span>
    {{# }else if(d.sex == 1){ }}
    <span>女</span>
    {{# } }}
</script>

<script type="text/html" id="user-login">
    {{#if (d.login == 0) { }}
    <span>在线</span>
    {{# }else if(d.sex == 1){ }}
    <span>离线</span>
    {{# } }}
</script>

<script type="text/html" id="user-createTime">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
</script>

<script src="${pageContext.request.contextPath}/resources/component/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/resources/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery','common','dtree','upload'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let dtree = layui.dtree;
        let upload = layui.upload;
        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '登陆名',
                    field: 'loginName',
                    align: 'center',
                    width:  100
                },
                {
                    title: '真实名称',
                    field: 'realName',
                    align: 'center',
                    width:  100
                },
                {
                    title: '性别',
                    field: 'sex',
                    align: 'center',
                    width:  80,
                    templet: '#user-sex'
                },
                {
                    title: '电话',
                    field: 'phone',
                    align: 'center'
                },
                {
                    title: '地址',
                    field: 'address',
                    align: 'center',
                    width:  80
                },
                {
                    title: '身份证号',
                    field: 'idCard',
                    align: 'center'
                },
                {
                    title: '状态',
                    field: 'enable',
                    field: 'deleted',
                    align: 'center',
                    templet: '#user-enable'
                },
                {
                    title: '创建时间',
                    field: 'createTime',
                    align: 'center',
                    templet: '#user-createTime'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '${pageContext.request.contextPath}/sysuser/findByPage.do',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.count, //解析数据长度
                    "data": res.data.data //解析数据列表
                }
            },
            response: {
                statusCode: 200
            }
        });

        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'assign') {
                window.assign(obj);
            }
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
            })
            return false;
        });

        form.on('switch(user-enable)', function(obj) {
            layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
        });
        // 给user-table 绑定一个事件
        form.on('switch(user-enable)', function (obj) {
            let data = {
                "id": obj.elem.getAttribute('user-id'),
                "deleted": obj.elem.checked ? 1 : 0
            }
            $.post("${pageContext.request.contextPath}/sysuser/updateUserStatus.do", data, function (res) {
                if (res.code != 200) {
                    layer.msg(res.msg) ;
                }
            })
        });
        window.assign = function (obj) {
            console.log(obj)
            layer.open({
                // 页面层
                type: 1,
                title: '分配角色',
                shade: 0.1,
                area: [common.isModile() ? '100%' : '500px', common.isModile() ? '100%' : '400px'],
                content: $("#assignTpl").html(),
                success: function (layerno, index) {
                    // 渲染树菜单
                    let roleTreeOpt = {
                        elem:"#roleTree" ,
                        checkbar: true,
                        checkbarType: 'self',
                        url:'${pageContext.request.contextPath}/sysrole/getUserRole.do?userId='+obj.data.id,
                        dataStyle: "layuiStyle",  //使用layui风格的数据格式
                        response:{message:"msg",statusCode:200},  //修改response中返回数据的定义
                    }
                    dtree.render(roleTreeOpt) ;

                    // 给按钮绑定事件
                    form.on('submit(user-assign)', function (data) {
                        var selectNodes =  dtree.getCheckbarNodesParam("roleTree")
                        var ids = [] ;
                        for(node of selectNodes){
                            ids.push(node.nodeId)
                        }
                        $.ajax({
                            url: '${pageContext.request.contextPath}/sysrole/assign.do?userId=' + obj.data.id,
                            data: JSON.stringify(ids),
                            dataType: 'json',
                            contentType: 'application/json',
                            type: 'post',
                            success: function (result) {
                                if (result.code === 200) {
                                    layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                        // 关闭当前的弹层
                                        layer.close(index);
                                        // 重新加载我们的表格
                                        table.reload("user-table");
                                    });
                                } else {
                                    layer.msg(result.msg, {icon: 2, time: 1000});
                                }
                            }
                        })
                        return false;
                    });
                }
            });
        }
        window.add = function() {
            layer.open({
                type: 1,
                title: '新增',
                shade: 0.1,
                area: [common.isModile() ? '100%' : '600px', common.isModile() ? '100%' : '500px'],
                content: $("#addTpl").html(),
                success: function (layerno,index) {
                    // 渲染form
                    form.render()
                    //常规使用 - 普通图片上传
                    var uploadInst = upload.render({
                        elem: '#test1'
                        ,url: '${pageContext.request.contextPath}/file/upload.do' //改成您自己的上传接口
                        ,before: function(obj){
                            //预读本地文件示例，不支持ie8
                            obj.preview(function(index, file, result){
                                $('#demo1').attr('src', result); //图片链接（base64）
                            });
                        },
                        done: function(res){
                            //如果上传失败
                            if(res.code !=200){
                                return layer.msg('上传失败');
                            }
                            $('#img').val(res.data); //置空上传失败的状态
                        }
                    });
                    // 给按钮绑定事件
                    form.on('submit(user-save)', function(data){
                        $.ajax({
                            url:'${pageContext.request.contextPath}/sysuser/add.do',
                            data:JSON.stringify(data.field),
                            dataType:'json',
                            contentType:'application/json',
                            type:'post',
                            success:function(result){
                                if(result.code === 200){
                                    layer.msg(result.msg,{icon:1,time:1000},function(){
                                        // 关闭当前的弹层
                                        layer.close(index);
                                        // 重新加载我们的表格
                                        table.reload("user-table");
                                    });
                                }else{
                                    layer.msg(result.msg,{icon:2,time:1000});
                                }
                            }
                        })
                        return false;
                    });
                }
            });
        }
        window.edit = function(obj) {
            layer.open({
                type: 1,
                title: '修改',
                shade: 0.1,
                area: ['600px', '500px'],
                content: $("#updateTpl").html(),
                success: function (layero, index) {
                    // 数据的回显update-user
                    form.val("update-user",obj.data);
                    $("#update-img").attr('src',obj.data.img);
                    $("#update-img-i").val(obj.data.img) ;
                    form.render() ;
                    //常规使用 - 普通图片上传
                    var uploadInst = upload.render({
                        elem: '#update-upload',
                        url: '${pageContext.request.contextPath}/file/upload.do',//改成您自己的上传接口
                        before: function(obj){
                            //预读本地文件示例，不支持ie8
                            obj.preview(function(index, file, result){
                                $('#update-img').attr('src', result); //图片链接（base64）
                            });
                        },
                        done: function(res){
                            //如果上传失败
                            if(res.code !=200){
                                return layer.msg('上传失败');
                            }
                            $('#update-img-i').val(res.data); //置空上传失败的状态
                        }
                    });
                    form.on('submit(user-save)', function(data){
                        $.ajax({
                            url:'${pageContext.request.contextPath}/sysuser/update.do',
                            data:JSON.stringify(data.field),
                            dataType:'json',
                            contentType:'application/json',
                            type:'post',
                            success:function(result){
                                if(result.code === 200){
                                    layer.msg(result.msg,{icon:1,time:1000},function(){
                                        layer.close(index);//关闭当前页
                                        table.reload("user-table");
                                    });
                                }else{
                                    layer.msg(result.msg,{icon:2,time:1000});
                                }
                            }
                        })
                        return false;
                    });
                }
            });
        }
        window.remove = function(obj) {
            layer.confirm('确定要删除该用户', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "${pageContext.request.contextPath}/sysuser/delete.do/" + obj.data['id'],
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.code === 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                obj.del();
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }
        window.batchRemove = function(obj) {
            var checkIds = common.checkField(obj,'id');
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            layer.confirm('确定要删除这些用户', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                console.log("aaa")
                $.ajax({
                    url: "${pageContext.request.contextPath}/sysuser/batchDelete.do/" + checkIds ,
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.code === 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }
        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
